package com.mate.cloud.redis.controller;


import com.mate.cloud.protocol.response.BaseResponse;
import com.mate.cloud.protocol.web.controller.AdminBaseController;
import com.mate.cloud.redis.constants.BloomFilterCache;
import com.mate.cloud.redis.constants.RedisKeyConstants;
import com.mate.cloud.redis.dal.model.City;
import com.mate.cloud.redis.service.ICityService;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/city")
public class CityController extends AdminBaseController {


    private final ICityService cityService;


    private final RedissonClient redissonClient;

    @GetMapping("/{id}")
    public BaseResponse<City> city(@PathVariable("id") Integer id) {
        City city = cityService.getById(id);
        return successBodyResponse(city);
    }

    @GetMapping("/redis/{id}")
    public BaseResponse<City> cityRedis(@PathVariable("id") Integer id) {

        return successBodyResponse(cityService.queryCityById(id));
    }

    /**
     * 布隆过滤器
     *
     * @param
     * @return
     * @author MI
     * @date 2025/8/31 18:33
     */
    @GetMapping("/bloom/{id}")
    public BaseResponse<City> filter(@PathVariable("id") Integer id) {
        String cityKey = String.format(RedisKeyConstants.CITY_KEY, id);

        if (BloomFilterCache.cityBloom.mightContain(cityKey)) {
            City city = cityService.queryCityById(cityKey, id);
            return successBodyResponse(city);
        }
        return successBodyResponse("缓存不存在！");
    }

    /**
     * 批量获取多个城市缓存
     *
     * @param
     * @return
     * @throws
     * @author MI
     * @date 2025/8/30 20:03
     */
    @PostMapping("/batchGetCity")
    public BaseResponse<City> batchGetCityCache(@RequestBody List<Integer> cityIds) {
        return successBodyResponse(cityService.batchGetCityCache(cityIds));
    }

    /**
     * 全量获取所有城市缓存（通过 Key 模板匹配所有符合的 Key）
     *
     * @param
     * @return
     * @throws
     * @author MI
     * @date 2025/8/30 20:01
     */
    @GetMapping("/cityAll")
    public BaseResponse<List<City>> cityAll() {
        return successBodyResponse(cityService.getAllCityCache());
    }
}
